﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DOAN.DO;
using DOAN.BL;

namespace DOAN
{
    public partial class Thi : System.Web.UI.Page
    {
        int socau;
        int maDT;
        string[] traloi = new string[200];
        string dad = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["TraLoiDung"] == null)
            {
                Session["TraLoiDung"] = 0;
            }
            if (Request.QueryString["MaDT"].ToString() != null)
            {

                
                string s = Request.QueryString["MaDT"].ToString();
                maDT = int.Parse(s);
                Session["MaDT"] = maDT;
                int a = getsocauhoi(maDT);
                Label2.Text = a.ToString();
                socau = int.Parse(Request.QueryString["Cau"]);

                string macauhoi = getcauhoi(maDT);
                string[] separator = new string[] { "-" };
                string[] maCH = macauhoi.Split(separator, StringSplitOptions.RemoveEmptyEntries);

                foreach (string ma in maCH)
                {
                    DataTable dt = new CauhoiBL().Select2(new CauhoiDO { Macauhoi = int.Parse(ma) });
                    if (dt.Rows.Count != 0)
                    {
                        Panel1.Visible = true;
                        Label1.Text = "<b>Câu hỏi " + socau + ": </b>" + dt.Rows[0].ItemArray[2].ToString();
                        RadioButton1.Text = dt.Rows[0].ItemArray[7].ToString();
                        RadioButton2.Text = dt.Rows[0].ItemArray[8].ToString();
                        RadioButton3.Text = dt.Rows[0].ItemArray[9].ToString();
                        RadioButton4.Text = dt.Rows[0].ItemArray[10].ToString();
                        dad = dt.Rows[0].ItemArray[11].ToString() + "-";
                    }
                    else
                    {
                        Panel1.Visible = false;
                        Label1.Text = "De thi hien tai chua co cau hoi";
                    }
                }
            }
        }
        private int getsocauhoi(int id)
        {
            return new DethiBL().Select(new DethiDO { Madethi = id }).Socau;
        }
        private string getcauhoi(int id)
        {
            return new DethiBL().Select(new DethiDO { Madethi = id }).Macauhoi;
        }
        private void nextch()
        {
            socau = socau + 1;
            string url = string.Format("Thi.aspx?MaDT={0}&Cau={1}", maDT, socau.ToString());
            if (socau == int.Parse(Label2.Text) + 1)
            {
                Response.Redirect("thongbao1.aspx");
            }
            else
            {
                Response.Redirect(url);
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (RadioButton1.Checked)
            {
                if (RadioButton1.Text == Label3.Text)
                {
                    Session["TraLoiDung"] = Convert.ToInt32(Session["TraLoiDung"]) + 1;
                    Response.Write(Session["TraLoiDung"].ToString());
                }
            }
            if (RadioButton2.Checked)
            {
                if (RadioButton2.Text == Label3.Text)
                {
                    Session["TraLoiDung"] = Convert.ToInt32(Session["TraLoiDung"]) + 1;
                    Response.Write(Session["TraLoiDung"].ToString());

                }
            }
            if (RadioButton3.Checked)
            {
                if (RadioButton3.Text == Label3.Text)
                {
                    Session["TraLoiDung"] = Convert.ToInt32(Session["TraLoiDung"]) + 1;
                    Response.Write(Session["TraLoiDung"].ToString());

                }
            }
            if (RadioButton4.Checked)
            {
                if (RadioButton4.Text == Label3.Text)
                {
                    Session["TraLoiDung"] = Convert.ToInt32(Session["TraLoiDung"]) + 1;
                    Response.Write(Session["TraLoiDung"].ToString());

                }
            }
            nextch();
        }
    }
}
